/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.List;
import org.springframework.web.servlet.mvc.AbstractRestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.*;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import model.Usuario;
import model.Viaje;
import service.GetUserCommand;
import service.UsuarioService;
import service.ViajeService;

public class UsuarioController extends AbstractRestController {

    private UsuarioService usuarioService;
    private ViajeService viajeService;
    private static Logger loggers = null;

    public void setUsuarioService(UsuarioService usuarioService) {
        this.usuarioService = usuarioService;
    }

    public void setViajeService(ViajeService viajeService) {
        this.viajeService = viajeService;
    }

    public UsuarioController() {
        super();

        // Make sure to set which method this controller will support.
        this.setSupportedMethods(new String[]{METHOD_GET});
    }

    protected ModelAndView handle(HttpServletRequest request,
            HttpServletResponse response, Object commandObj, BindException errors)
            throws Exception {

        List users;
        List via;
        Usuario user;
        GetUserCommand command = (GetUserCommand) commandObj;

        users = usuarioService.getUsuarioUsername(command.getName());
        user = (Usuario) users.get(0);
        via = viajeService.listdeMisViajes(user.getIdUsuario());
        System.out.println("hola aqui voy en USUARIOOOO");
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", user);
        mav.addObject("viaje", via);
        return mav;
    }
}
